﻿// -----------------------------------------------------------------------
// <copyright file="ComparerPreview.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Sitecore.ChangesTracker.UI
{
  using System;
  using System.Collections;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Web.UI.WebControls;
  using Sitecore.ChangesTracker.Archiving;
  using Sitecore.Configuration;
  using Sitecore.Data.Items;
  using Sitecore.Diagnostics;
  using Sitecore.Pipelines;
  using Sitecore.Pipelines.GetContentEditorWarnings;
  using Sitecore.Shell.Applications.ContentEditor.Pipelines.GetContentEditorFields;
  using Sitecore.Shell.Applications.ContentEditor.Pipelines.RenderContentEditor;
  using Sitecore.Shell.Applications.ContentManager.Dialogs.ResetFields;
  using Sitecore.Web.UI.HtmlControls;
  using Sitecore.Web.UI.WebControls;
    using Sitecore.Text;
    using Sitecore.Shell;
    using System.Web.UI;
    using System.IO;
    using Sitecore.Globalization;
    using Sitecore.SecurityModel;

  /// <summary>
  /// TODO: Update summary.
  /// </summary>
  public class ComparerPreview : Sitecore.Shell.Applications.ContentManager.Editor
  {
    private Item item;
    private ArchivedItem arc_item;
    private ViewItemInfo cp;
     public ComparerPreview()
     {
       base.RenderWarnings = false;
     }
    public virtual void RenderComparer(Item item, ArchivedItem arc_item, System.Web.UI.Control parent, ViewItemInfo controlPoint,ChangeTrackerArchiveEntry entry)
    {
      Assert.ArgumentNotNull(parent, "parent");
      Assert.ArgumentNotNull(item, "item");
      Assert.ArgumentNotNull(arc_item, "arc_item");
      Assert.ArgumentNotNull(controlPoint, "controlPoint");
      this.item = item;
      this.arc_item = arc_item;
      this.cp = controlPoint;
      Sections sections = this.GetSections();
      RenderContentEditorArgs args = new RenderContentEditorArgs
      {
        EditorFormatter = this.GetEditorFormatter(),
        Item = this.item,
        Parent = parent,
        Sections = sections,
        ReadOnly = true,
        Language = this.Language,
        IsAdministrator = this.IsAdministrator
        
        
      };


      RenderQuickInfo(args, arc_item, entry);
      if (item.TemplateID.ToGuid() == arc_item.TemplateId)
      {
          this.Render(args, parent);
      }
      else
      {
          HtmlTextWriter output = new HtmlTextWriter(new StringWriter());
          output.Write("<h1>Template was changed. Nothing to compare.</h1>");
          args.EditorFormatter.AddLiteralControl(args.Parent, output.InnerWriter.ToString());
      }
    }

   


    protected override Shell.Applications.ContentManager.Editor.Sections GetSections()
    {
      Sections sections = new Sections();
      GetContentEditorFieldsArgs args = new GetContentEditorFieldsArgs(this.item)
      {
        Sections = sections,
        FieldInfo = new Hashtable(),
        ShowDataFieldsOnly = false
      };
      using (new LongRunningOperationWatcher(Settings.Profiling.RenderFieldThreshold, "getContentEditorFields pipeline[id={0}]", new string[] { this.item.ID.ToString() }))
      {
        CorePipeline.Run("getContentEditorFields", args);
      }
      return sections;
    }
    protected override Sitecore.Pipelines.GetContentEditorWarnings.GetContentEditorWarningsArgs GetWarnings(bool hasSections)
    {
      GetContentEditorWarningsArgs args = new GetContentEditorWarningsArgs(this.item)
      {
        ShowInputBoxes = this.ShowInputBoxes,
        HasSections = hasSections
      };
      using (new LongRunningOperationWatcher(Settings.Profiling.RenderFieldThreshold, "GetContentEditorWarningsArgs pipeline", new string[0]))
      {
        CorePipeline.Run("getContentEditorWarnings", args);
      }
      return args;

    }
    protected override Shell.Applications.ContentEditor.EditorFormatter GetEditorFormatter()
    {
      return new ComparerFormatter(arc_item,/* item,*/ cp);
    }
    protected override bool GetReadOnly(Item item)
    {
      return true;
    }
    private static void RenderQuickInfo(RenderContentEditorArgs args, ArchivedItem arc_item,ChangeTrackerArchiveEntry entry)
    {
        Assert.ArgumentNotNull(args, "args");
        Item item = args.Item;
        if (item != null)
        {
            bool isQuickInfoSectionCollapsed = IsQuickInfoSectionCollapsed;
            bool renderCollapsedSections = UserOptions.ContentEditor.RenderCollapsedSections;
            
            if (renderCollapsedSections || !isQuickInfoSectionCollapsed)
            {
                HtmlTextWriter output = new HtmlTextWriter(new StringWriter());
                output.Write("<table width=\"100%\" cellpadding=\"4\" cellspacing=\"0\" border=\"0\">");
                output.Write("<col style=\"white-space:nowrap\" align=\"left\" valign=\"top\" />");
                output.Write("<col style=\"white-space:nowrap\" valign=\"top\" align=\"left\" />");
               
                RenderQuickInfoID(output, item);
                
                RenderQuickInfoItemKey(output, item,arc_item);
                RenderQuickInfoPath(output, item,entry.OriginalLocation);
                RenderQuickInfoTemplate(output, item,arc_item);
                //RenderQuickInfoCreatedFrom(output, item);
                //RenderQuickInfoOwner(output, item);
                output.Write("</table>");
                args.EditorFormatter.AddLiteralControl(args.Parent, output.InnerWriter.ToString());
            }
            
        }
    }

    private static void RenderQuickInfoID(HtmlTextWriter output, Item item)
    {
        Assert.ArgumentNotNull(output, "output");
        Assert.ArgumentNotNull(item, "item");
        output.Write("<tr><td>");
        output.Write(Translate.Text("Item ID:"));
        output.Write("</td><td>");
        output.Write("<input class=\"scEditorHeaderQuickInfoInput\" readonly=\"readonly\" onclick=\"javascript:this.select();return false\" value=\"" + item.ID + "\"/>");
        output.Write("</td></tr>");
    }

    private static void RenderQuickInfoItemKey(HtmlTextWriter output, Item item, ArchivedItem arc_item)
    {
        Assert.ArgumentNotNull(output, "output");
        Assert.ArgumentNotNull(item, "item");
        output.Write("<tr><td>");
        output.Write(Translate.Text("Item Name:"));
        output.Write("</td><td>");
        output.Write(item.Name);      
        output.Write("</td>");
        output.Write("<td>");
        output.Write(Translate.Text("Item Name:"));
        output.Write("</td>");
        string style = string.Empty;
        if (item.Name != arc_item.Name)
        {
            style = "style=\"color:red;\"";
        }
        output.Write("<td "+style+">");
        output.Write(arc_item.Name);
        output.Write("</td>");
        output.Write("</tr>");
    }

    private static void RenderQuickInfoPath(HtmlTextWriter output, Item item, string originalLocation)
    {
        Assert.ArgumentNotNull(output, "output");
        Assert.ArgumentNotNull(item, "item");
        output.Write("<tr>");
        output.Write("<td>");
        output.Write(Translate.Text("Item Path:"));
        output.Write("</td>");
        output.Write(" <td>");
        output.Write("<input class=\"scEditorHeaderQuickInfoInput\" readonly=\"readonly\" onclick=\"javascript:this.select();return false\" value=\"" + item.Paths.Path + "\"/>");
        output.Write("</td>");
        output.Write("<td>");
        output.Write(Translate.Text("Item Path:"));
        output.Write("</td>");
        output.Write(" <td>");
        string style = string.Empty;
        if (item.Paths.Path != originalLocation)
        {
            style = "style=\"color:red;\"";
        }
        output.Write("<input class=\"scEditorHeaderQuickInfoInput\" readonly=\"readonly\""+style+" onclick=\"javascript:this.select();return false\" value=\"" + originalLocation + "\"/>");
        output.Write("</td>");   
        output.Write("</tr>");
    }
    private static void RenderQuickInfoTemplate(HtmlTextWriter output, Item item,ArchivedItem arc_item)
    {
        Item item2;
        Assert.ArgumentNotNull(output, "output");
        Assert.ArgumentNotNull(item, "item");
        output.Write("<tr>");
        output.Write("<td>");
        output.Write(Translate.Text("Template:"));
        output.Write("</td>");
        output.Write("<td>");
        using (new SecurityDisabler())
        {
            item2 = item.Database.GetItem(item.TemplateID);
        }
        if (item2 != null)
        {
            output.Write(item2.Paths.Path);
        }
        else
        {
            output.Write(Translate.Text("[template no longer exists]"));
        }
        output.Write(" - ");
        output.Write("<input class=\"scEditorHeaderQuickInfoInputID\" readonly=\"readonly\" value=\"" + item.TemplateID + "\"/>");
        output.Write("</td>");

        output.Write("<td>");
        output.Write(Translate.Text("Template:"));
        output.Write("</td>");
        output.Write("<td>");
        using (new SecurityDisabler())
        {
            item2 = item.Database.GetItem(Sitecore.Data.ID.Parse(arc_item.TemplateId));
        }
        if (item2 != null)
        {
            output.Write(item2.Paths.Path);
        }
        else
        {
            output.Write(Translate.Text("[template no longer exists]"));
        }
        string style = string.Empty;
        if (arc_item.TemplateId != item.TemplateID.ToGuid())
        {
            style = "style=\"color:red;\"";
        }
        output.Write(" - ");
        output.Write("<input class=\"scEditorHeaderQuickInfoInputID\" "+ style + " readonly=\"readonly\" value=\"{" + arc_item.TemplateId + "}\"/>");
        output.Write("</td>");
        output.Write("</tr>");
    }

      private static bool IsQuickInfoSectionCollapsed
    {
        get
        {
            UrlString str = new UrlString(Registry.GetString("/Current_User/Content Editor/Sections/Collapsed"));
            string str2 = str["QuickInfo"];
            return (string.IsNullOrEmpty(str2) || (str2 == "1"));
        }
    }

  }
}
